﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Admin.Business.Files
{
    public static class Index
    {
        public static Adams.Admin.DataTransfer.Files.Index.PageLoad PageLoad()
        {
            return DataAccess.Files.Index.PageLoad();
        }

        public static Adams.Admin.DataTransfer.Files.Index.GridLoad GridLoad(DataTransfer.GridLoadInput gridinput)
        {
            return DataAccess.Files.Index.GridLoad(gridinput);
        }

        public static void Delete(int id)
        {
            DataAccess.Files.Index.Delete(id);
        }

        public static void Upload(Adams.Admin.DataTransfer.Files.Index.UploadedFile uploadedFile)
        {
            int counter = 0;
            uploadedFile.Url = uploadedFile.Name;
            var fileinfo = DataAccess.Files.Create.GetFileInfo(uploadedFile.Url, 0); ;

            do
            {
                counter++;
                fileinfo = DataAccess.Files.Create.GetFileInfo(uploadedFile.Url, 0);
                if (fileinfo.Exists)
                {
                    uploadedFile.Url = string.Format("{1}-{0}", uploadedFile.Name, counter);
                }
            }
            while (fileinfo.Exists);

            DataAccess.Files.Index.Upload(uploadedFile);
        }

        public static Adams.Shared.Entities.Files.File GetFile(int fileId)
        {
            return DataAccess.Files.Index.GetFile(fileId);
        }
    }
}
